/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Java_IO_File_Input_Output_Stream.java]
 * Creation Date        [07-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_09_io.api;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * java.io.FileInputStream<br>
 * java.io.FileOutputSteam<br>
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_IO_File_Input_Output_Stream {

	/**
	 * 使用FileInputStream & FileOutputStream拷贝文件
	 * 
	 * @param fileSource
	 * @param fileDest
	 */
	public static void copyFileByFileStream(String fileSource, String fileDest) {
		FileInputStream in = null;
		FileOutputStream out = null;

		try {
			in = new FileInputStream(fileSource);
			out = new FileOutputStream(fileDest);

			byte[] buffer = new byte[2048];
			int length = 0;
			while ((length = in.read(buffer)) != -1) {
				out.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 先关闭输出流, 再关闭输入流
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
